package com.rede.didiok.community.web;

import java.util.List;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.community.UserMomentDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.community.service.UserMomentService;



/**
 * 用户动态表
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-09-06 18:35:23
 */
@Slf4j
@RestController
@RequestMapping("community/feign/userMoment")
public class UserMomentController {
    @Autowired
    private UserMomentService userMomentService;


    /**
     * 根据用户列表ID  查询各用户发表博客数
     * @param userUids
     * @return
     */
    @GetMapping("/getUserMomentCountByUserIds")
    R getUserMomentCountByUserIds(@RequestParam("userUids") List<String> userUids){
        return R.ok().setData(userMomentService.getUserMomentCountByUserIds(userUids));
    }

    /**
     * 用户动态下架
     * @param uid
     * @return
     */
    @PostMapping("/revocation")
    R revocationMoment(@RequestParam("uid") String uid){
        return R.ok().setData(userMomentService.revocationMoment(uid));
    }

    /**
     * 批量同步用户动态索引
     * @param
     * @return
     */
    @PostMapping("/buildAggDoc")
    String buildMomentAggDoc(){
        return userMomentService.buildMomentAggDoc();
    }


    @ApiOperation(value = "获取用户动态列表", notes = "获取用户动态列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody UserMomentDto userMomentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户动态列表");
        return ResultUtil.successWithData(userMomentService.getPageList(userMomentDto));
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加用户动态", notes = "增加用户动态", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody UserMomentDto userMomentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加用户动态");
        return userMomentService.addUserMoment(userMomentDto);
    }

    @ApiOperation(value = "编辑用户动态", notes = "编辑用户动态", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody UserMomentDto userMomentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑用户动态");
        return userMomentService.editUserMoment(userMomentDto);
    }

    @ApiOperation(value = "批量删除用户动态", notes = "批量删除用户动态", response = String.class)
    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<UserMomentDto> userMomentDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("批量删除用户动态");
        return userMomentService.deleteBatchUserMoment(userMomentDtoList);
    }

    @ApiOperation(value = "审核用户动态", notes = "审核用户动态", response = String.class)
    @PostMapping("/audit")
    public String audit(@RequestBody UserMomentDto userMomentDto) {
        return userMomentService.auditMoment(userMomentDto);
    }

    /**
     * 根据id获取用户动态
     * @param
     * @return
     */
    @GetMapping(value = "/getById")
    R getUserMomentById(@RequestParam("uid") String uid){
        return R.ok().setData(userMomentService.getUserMomentById(uid));
    }

    /**
     * 获取动态数目
     *
     * @param userMomentVO
     * @return
     */
    @GetMapping("/getCount")
    Integer getUserMomentCount(@RequestParam("userUid") String userUid){
        return userMomentService.getUserMomentCount(userUid);
    }

    /**
     * 根据id集合查询用户动态
     * @param uidList
     * @return
     */
    @GetMapping("/listUserMomentByUidList")
    R listUserMomentByUidList(@RequestParam("uidList") List<String> uidList){
        return R.ok().setData(userMomentService.listUserMomentByUidList(uidList));
    }
}
